package com.ssm.basis.model.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.PlatformTransactionManagerCustomizer;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;

import static com.ssm.basis.configuration.ConfigurationPackage.MAPPER_PACKAGE;

/**
 * @see TransactionAutoConfiguration
 * @see DataSourceTransactionManagerAutoConfiguration
 * @see MybatisPlusAutoConfiguration
 */
// @EnableTransactionManagement
@MapperScan(MAPPER_PACKAGE)
@Configuration(proxyBeanMethods = false)
public class MybatisPlusConfig {
    @Bean
    MybatisPlusInterceptor mybatisPlusInterceptor() { // 插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
        return interceptor;
    }

    PaginationInnerInterceptor paginationInnerInterceptor() { // 分页
        return new PaginationInnerInterceptor(DbType.MYSQL);
    }

    BlockAttackInnerInterceptor blockAttackInnerInterceptor() { // 防止全表更新与删除
        return new BlockAttackInnerInterceptor();
    }

    // @Bean
    // public ISqlInjector sqlInjector() {
    //     return new DefaultSqlInjector() {
    //         @Override
    //         public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    //             List<AbstractMethod> methodList = super.getMethodList(mapperClass);
    //             methodList.add(new LogicDeleteByIdWithFill());
    //             return methodList;
    //         }
    //     };
    // }

    @Bean
    ConfigurationCustomizer configurationCustomizer() {
        // Map 下划线自动转驼峰
        return i -> i.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    }

    @Bean
    PlatformTransactionManagerCustomizer<DataSourceTransactionManager> platformTransactionManagerCustomizer() {
        // org.springframework.jdbc.datasource.AbstractDataSource
        return transactionManager -> transactionManager.setDataSource(new LazyConnectionDataSourceProxy(transactionManager.getDataSource()));
    }
}
